package org.news.web.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dtflys.forest.annotation.Get;
import lombok.RequiredArgsConstructor;
import org.apache.poi.util.IOUtils;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.service.OssService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.impl.SysOssServiceImpl;
import org.news.web.constant.NewsConstant;
import org.news.web.domain.News;
import org.news.web.domain.bo.NewsBo;
import org.news.web.domain.bo.PythonNewsBo;
import org.news.web.domain.vo.NewsVo;
import org.news.web.mapper.NewsMapper;
import org.news.web.service.INewsService;
import org.news.web.service.impl.NewsServiceImpl;
import org.news.web.utils.FileUtils;
import org.news.web.utils.HotRankUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RJsonBucket;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JacksonCodec;
import org.redisson.codec.JsonCodec;

import org.springframework.mock.web.MockMultipartFile;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Validated
@SaIgnore
@RestController
@RequiredArgsConstructor
public class testController {


    private final RedisTemplate redisTemplate;

    private final INewsService iNewsService;

    private final NewsServiceImpl newsService;

    private final NewsMapper newsMapper;

    private final SysOssServiceImpl ossService;

    private final HotRankUtils hotRankUtils;




    @PostMapping("/test/KeyWord")
    public R<String> keyWord(@RequestBody NewsBo bo){
        System.out.println(bo);
       return R.ok(iNewsService.getRelateKey(bo));
    }



    @GetMapping("/test/redis/{key}")
    public R<List<PythonNewsBo>> testsd(@PathVariable("key") String key) {
        RedissonClient client = RedisUtils.getClient();
        RBucket<String> bucket = client.getBucket(key, null);
//        System.out.println(bucket.getName());
//        System.out.println(bucket.get());

        List<PythonNewsBo> pythonNewsBos = JSONObject.parseArray(bucket.get(), PythonNewsBo.class);
//        把time 转换成data
//        05/08/2024 08:02:05
//        MM/dd/yyyy HH:mm:ss
        Date date = DateUtil.parse(pythonNewsBos.get(0).getTime(), "MM/dd/yyyy HH:mm:ss");
        System.out.println(date);
//       把url上传到oss中
        String coverUrl = pythonNewsBos.get(0).getCoverUrl();
        System.out.println(coverUrl);
        MultipartFile multipartFile = FileUtils.UrlToMultpartFile(coverUrl);
        SysOssVo upload = ossService.upload(multipartFile);
        System.out.println(upload);


        return R.ok(pythonNewsBos);
//        return R.ok(null);
    }


    @GetMapping("/test/info")
    public R<Void> com() throws UnsupportedEncodingException, ClientException {
        hotRankUtils.UpdateNewsInfo();
        return R.ok();
    }


    @GetMapping("/test/updateNewsContent")
    public R<Void> updateNews(){
        List<News> news = newsMapper.selectList(null);
        System.out.println(news.size());
        for (News news1 : news) {
            if(StringUtils.isNotBlank(news1.getContent())){
                news1.setContent(HotRankUtils.htmlToMd(news1.getContent()));
            }
        }
        newsMapper.updateBatchById(news);
        return R.ok();
    }


    @GetMapping("/test/update")
    public R<Void> update(){
        List<News> news = newsMapper.selectList(null);
        System.out.println(news.size());
        for (News news1 : news) {
            if(StringUtils.isNotBlank(news1.getContent())){

                news1.setContent(news1.getContent().replaceAll("\\{#.*?\\}",""));
            }
        }
        newsMapper.updateBatchById(news);
        return R.ok();
    }



    @GetMapping("/test/jb")
    public R jb(){
        HotRankUtils.pythonRun();

        return R.ok();
    }


    @GetMapping("/test/XGD")
    public R testXGD(@RequestParam("a") String a,@RequestParam("b")String b){
        return R.ok(newsService.XGD(a.split(","),b.split(",")));
    }


    @GetMapping("/test/DS")
    public R testDS(){
        //        获取所有已经审核通过的新闻
        List<News> newsVos = newsMapper.selectList(new LambdaQueryWrapper<News>().eq(News::getApplyStatus, NewsConstant.shenHeTong));

        List<News> news = new ArrayList<>();
        for (News item : newsVos) {
            if(item.getPublishTime() == null || new Date().getTime() >= item.getPublishTime().getTime()){
                //            更新状态为发布
                item.setApplyStatus(NewsConstant.FaBu);
                news.add(item);
            }
        }
        if(news.size() >0){
            //        批量更新
            newsMapper.updateBatchById(news);
        }
        return R.ok();
    }









}
